﻿using System.Runtime.CompilerServices;
using NUnit.Framework;
using ICSharpCode.Decompiler.Tests.Helpers;
using System.CodeDom.Compiler;
using System.IO;

namespace ICSharpCode.Decompiler.Tests.BambomLuaTest
{
	[TestFixture, Parallelizable(ParallelScope.All)]
	public class TestHellowWorldRunner
	{
		static readonly string TestCasePath = BTester.TestCasePath + "/Correctness";
		[Test]
		public void TestLoop()
		{
			RunLuaEx("TestLoop");
		}
		[Test]
		public void HollowWorldTests()
		{
			RunCSEx("HollowWorldTest");
		}
		[Test]
		public void BComparisons()
		{
			RunCSEx("BComparisons");
		}
		void RunCSEx(string testName = null, CompilerOptions options = CompilerOptions.UseDebug)
		{
			string testFileName = testName + ".cs";
			string TestCaseOutputFolder = Path.Combine(TestCasePath, testName + "_TestOutputFolder");
			if (Directory.Exists(TestCaseOutputFolder)) {
				Directory.Delete(TestCaseOutputFolder, true);
			}
			Directory.CreateDirectory(TestCaseOutputFolder);

			string testOutputFileName = testName + BTester.GetSuffix(options) + ".exe";
			CompilerResults outputFile = null;

			//编译cs文件到exe文件
			outputFile = BTester.CompileCSharp(Path.Combine(TestCasePath, testFileName), options,
			outputFileName: Path.Combine(TestCaseOutputFolder, testOutputFileName));

			string decompileOutputFileName = Path.Combine(TestCaseOutputFolder, testFileName);
			//开始反编译文件
			BTester.DecompileCSharpEx(outputFile.PathToAssembly, decompileOutputFileName, BTester.GetSettings(options));
		}
		void RunLuaEx(string testName = null, CompilerOptions options = CompilerOptions.UseDebug)
		{
			string testFileName = testName + ".cs";
			string TestCaseOutputFolder = Path.Combine(TestCasePath, testName + "_TestOutputFolder");
			if (Directory.Exists(TestCaseOutputFolder)) {
				Directory.Delete(TestCaseOutputFolder, true);
			}
			Directory.CreateDirectory(TestCaseOutputFolder);

			string testOutputFileName = testName + BTester.GetSuffix(options) + ".exe";
			CompilerResults outputFile = null;

			//编译cs文件到exe文件
			outputFile = BTester.CompileCSharp(Path.Combine(TestCasePath, testFileName), options,
			outputFileName: Path.Combine(TestCaseOutputFolder, testOutputFileName));

			string decompileOutputFileName = Path.Combine(TestCaseOutputFolder, testFileName);
			//开始反编译文件
			BTester.DecompileLuaEx(outputFile.PathToAssembly, decompileOutputFileName, BTester.GetSettings(options));
		}
	}
}
